//
//  MineMessageSafeController.m
//  Coach_HaiGeEr
//
//  Created by liuchen on 2018/9/13.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "MineMessageSafeController.h"
#import "LoginFindSecretController.h"
#import "LoginBySecretController.h"
#import "MineController.h"

@interface MineMessageSafeController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,copy) UITableView *tableView;
@property (nonatomic,copy) NSArray *titleArr;
@end

@implementation MineMessageSafeController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[CoachTabBarController shareLZHTabbarController].tabBar setHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavBack];
    [self createSubViews];
}

#pragma mark ==========创建视图==========
- (void)createSubViews{
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"帐号安全";
    self.titleArr = @[@"当前帐号",@"修改密码"];
    [self.view addSubview:self.tableView];
}

- (UITableView *)tableView{
    
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, SYSTEM_TOP, SCREEN_WIDTH, SCREEN_HEIGHT - SYSTEM_TOP) style:UITableViewStylePlain];
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 30*SCALEUNIT, 0, 30*SCALEUNIT)];
        [_tableView setSeparatorColor:RGBA(230, 230, 230, 1)];
    }
    return _tableView;
}

#pragma mark ==========UITableView代理方法==========
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.titleArr.count;
    }else {
        return 1;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 95*SCALEUNIT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *returnCell = nil;
    if (indexPath.section == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineMessageSafeControllerone"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"MineMessageSafeControllerone"];
            cell.textLabel.textColor = RGBA(70, 70, 70, 1);
            cell.textLabel.font = [UIFont systemFontOfSize:LoginFont-2];
            cell.detailTextLabel.textColor = RGBA(70, 70, 70, 1);
            cell.detailTextLabel.font = [UIFont systemFontOfSize:LoginFont-2];
        }
        cell.textLabel.text = self.titleArr[indexPath.row];
        if (indexPath.row == 1) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }else{
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.detailTextLabel.text = [UserMessage sharedInstance].model.phonesNo==0?@"":[[NSString stringWithFormat:@"%ld",[UserMessage sharedInstance].model.phonesNo] stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
        }
        returnCell = cell;
    }else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineMessageSafeControllerTwo"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MineMessageSafeControllerTwo"];
            cell.textLabel.text = @"退出登录";
            cell.textLabel.textColor = RGBA(25, 25, 25, 1);
            cell.textLabel.textAlignment = NSTextAlignmentCenter;
            cell.textLabel.font = [UIFont systemFontOfSize:LoginFont+1];
        }
        returnCell = cell;
    }
    
    return returnCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        if (indexPath.row==1) {
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            LoginFindSecretController *vc = [[LoginFindSecretController alloc] init];
            vc.jumpNum = 1;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }else {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        [self loginOut];
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *returnView = nil;
    if (section == 1) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = RGBA(230, 230, 230, 1);
        returnView = view;
    }
    return returnView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    CGFloat hegiht = 0;
    if (section == 1) {
        hegiht = 20*SCALEUNIT;
    }
    return hegiht;
}

#pragma mark ==========退出登录==========
- (void)loginOut{
    [UserMessage sharedInstance].model = nil;
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userMessage"];
    if ([UserMessage sharedInstance].model==nil && [[NSUserDefaults standardUserDefaults] objectForKey:@"userMessage"]==nil) {
        [self showMessage:@"退出登录成功"];
        [self performSelector:@selector(jumpToLoginPage) withObject:nil afterDelay:1.5f];
    }else {
        [self showMessage:@"退出登录失败"];
    }
}

- (void)jumpToLoginPage{
    [[[UIApplication sharedApplication].delegate window].rootViewController removeFromParentViewController];
    [self.navigationController popToRootViewControllerAnimated:YES];
    [[UIApplication sharedApplication].delegate window].rootViewController = [[UINavigationController alloc] initWithRootViewController:[[LoginBySecretController alloc] init]];
    [[[UIApplication sharedApplication].delegate window] makeKeyAndVisible];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc{
    NSLog(@"释放");
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
